package com.pig4cloud.pig.admin.controller;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pig.admin.api.entity.BsAccountManageEntity;
import com.pig4cloud.pig.admin.api.entity.BsAircorpManageEntity;
import com.pig4cloud.pig.admin.api.entity.BsGiftEntity;
import com.pig4cloud.pig.admin.service.BsAccountManageService;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.common.security.annotation.Inner;
import org.springframework.security.access.prepost.PreAuthorize;
import com.pig4cloud.plugin.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 账户
 *
 * @author pig
 * @date 2025-04-03 17:43:34
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/bsAccountManage" )
@Tag(description = "bsAccountManage" , name = "账户管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class BsAccountManageController {

    private final BsAccountManageService bsAccountManageService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param bsAccountManage 账户
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
	@Inner(value = false)
    public R getBsAccountManagePage(@ParameterObject Page page, @ParameterObject BsAccountManageEntity bsAccountManage) {
        LambdaQueryWrapper<BsAccountManageEntity> wrapper = Wrappers.lambdaQuery();
		wrapper.orderByDesc(BsAccountManageEntity::getBuyTime);
        return R.ok(bsAccountManageService.page(page, wrapper));
    }

	/**
	 * 分页查询
	 * @param bsAccountManage 账户
	 * @return
	 */
	@Operation(summary = "分页查询" , description = "分页查询" )
	@GetMapping("/list" )
	@Inner(value = false)
	public R list( @ParameterObject BsAccountManageEntity bsAccountManage) {
		LambdaQueryWrapper<BsAccountManageEntity> wrapper = Wrappers.lambdaQuery();
		return R.ok(bsAccountManageService.list( wrapper));
	}


    /**
     * 通过id查询账户
     * @param accountId id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{accountId}" )
	@Inner(value = false)
    public R getById(@PathVariable("accountId" ) Long accountId) {
        return R.ok(bsAccountManageService.getById(accountId));
    }

    /**
     * 新增账户
     * @param bsAccountManage 账户
     * @return R
     */
    @Operation(summary = "新增账户" , description = "新增账户" )
    @SysLog("新增账户" )
    @PostMapping
	@Inner(value = false)
    public R save(@RequestBody BsAccountManageEntity bsAccountManage) {
        return R.ok(bsAccountManageService.saveAccount(bsAccountManage));
    }

    /**
     * 修改账户
     * @param bsAccountManage 账户
     * @return R
     */
    @Operation(summary = "修改账户" , description = "修改账户" )
    @SysLog("修改账户" )
    @PutMapping
	@Inner(value = false)
    public R updateById(@RequestBody BsAccountManageEntity bsAccountManage) {
        return R.ok(bsAccountManageService.updateById(bsAccountManage));
    }

    /**
     * 通过id删除账户
     * @param ids accountId列表
     * @return R
     */
    @Operation(summary = "通过id删除账户" , description = "通过id删除账户" )
    @SysLog("通过id删除账户" )
    @DeleteMapping
	@Inner(value = false)
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(bsAccountManageService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     * @param bsAccountManage 查询条件
   	 * @param ids 导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
	@Inner(value = false)
    public List<BsAccountManageEntity> export(BsAccountManageEntity bsAccountManage,Long[] ids) {
        return bsAccountManageService.list(Wrappers.lambdaQuery(bsAccountManage).in(ArrayUtil.isNotEmpty(ids), BsAccountManageEntity::getAccountId, ids));
    }

	/**
	 * 初始化管理账号，
	 * @param bsAccountManage
	 * @return
	 */
	@PostMapping("/initAccount")
	@Inner(value = false)
	public R initBalace(BsAccountManageEntity bsAccountManage) {
		return R.ok(bsAccountManageService.initBalace(bsAccountManage));
	}
	
	/**
	 * 赠送管理
	 * @param bsGiftEntity
	 * @return
	 */
	@PostMapping("/giftAccount")
	@Inner(value = false)
	public R giftBalace(BsGiftEntity bsGiftEntity) {
		if (bsAccountManageService.giftBalace(bsGiftEntity)){
			return R.ok("赠送成功");
		}else {
			return R.failed("赠送成功");
		}

	}

}